/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.tta;

import session.TX.TTADisplaySBRemote;
import helper.tx.tta.Helper_PIPAd;
import helper.tx.tta.Helper_PIPAdModel;
import helper.tx.tta.Helper_PIPSales;
import helper.tx.tta.Helper_PIPSalesPred;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.model.chart.CartesianChartModel;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "spipAdsMB")
@ViewScoped
public class SPIPAdsMB {

    @EJB
    TTADisplaySBRemote pipBean;
    private Calendar start;
    private Calendar end;
    private List<Calendar> possibleMonths;
    private Set<Integer> startMonths = new HashSet<Integer>();
    private List<Integer> startYears = new ArrayList<Integer>();
    private List<Integer> endYears = new ArrayList<Integer>();
    private Set<Integer> endMonths = new HashSet<Integer>();
    private Integer adsYearStart;
    private Integer adsMonthStart;
    private Integer adsYearEnd;
    private Integer adsMonthEnd;
    
    private double saleCorCoef;
    private Helper_PIPSalesPred predItem;
    private NumberFormat formatter;
    
    public SPIPAdsMB(){
        formatter = new DecimalFormat("#0.0000");
    }

    public List<Helper_PIPSales> getSales() {
        String stakeholderName = getStakeholderName();
        if (stakeholderName == null) {
            return null;
        }
        possibleMonths = this.getPossibleMonths();
        if (possibleMonths == null || possibleMonths.isEmpty()) {
            return null;
        }
        if (start == null || end == null) {
            start = possibleMonths.get(0);
            end = possibleMonths.get(possibleMonths.size() - 1);
        }

        List<Helper_PIPSales> returnList = pipBean.getSSalesData(stakeholderName, start, end);
//        System.out.println("start:" + start.get(Calendar.YEAR) + ", " + start.get(Calendar.MONTH));
//        System.out.println("end:" + end.get(Calendar.YEAR) + ", " + end.get(Calendar.MONTH));
        return returnList;
    }

    public CartesianChartModel getAdsModel() {
        System.out.println("getting adds model");
        String stakeholderName = getStakeholderName();
        if (stakeholderName == null) {
            return null;
        }
        possibleMonths = this.getPossibleMonths();
        if (possibleMonths == null || possibleMonths.isEmpty()){
            return new CartesianChartModel();
        }
        if (start == null || end == null) {
            start = possibleMonths.get(0);
            end = possibleMonths.get(possibleMonths.size() - 1);
        }
        System.out.println(start.get(Calendar.YEAR) + ", " + start.get(Calendar.MONTH));
        System.out.println(end.get(Calendar.YEAR) + ", " + end.get(Calendar.MONTH));
        List<Helper_PIPAdModel> returnList = pipBean.getAdsData(stakeholderName, start, end);
        CartesianChartModel model = new CartesianChartModel();
        if (returnList == null || returnList.isEmpty()) {
            return model;
        }
        ChartSeries ads ;
        for (int i = 0; i < returnList.size(); i++) {
            Helper_PIPAdModel aHelper = returnList.get(i);
            System.out.println(aHelper.getAds().getTitle());
            ads = new ChartSeries(aHelper.getAds().getTitle());
            ads.setLabel(aHelper.getAds().getTitle()); 
            for (Helper_PIPAd aRecord: aHelper.getAdsRecords()){
                System.out.println(aRecord.getNumClicks());
                ads.set(aRecord.getTimeStr(), aRecord.getNumClicks());
            }
            model.addSeries(ads); 
            System.out.println("adding a series");
        }
        
        return model;
    }

    public Calendar getEnd() {
        return end;
    }

    public void setEnd(Calendar end) {
        this.end = end;
    }

    public Calendar getStart() {
        return start;
    }

    public void setStart(Calendar start) {
        this.start = start;
    }

    public Integer getAdsMonthStart() {
        return adsMonthStart;
    }

    public void setAdsMonthStart(Integer salesMonth) {
        this.adsMonthStart = salesMonth;
    }

    public Integer getAdsYearStart() {
        return adsYearStart;
    }

    public void setAdsYearStart(Integer salesYear) {
        this.adsYearStart = salesYear;
    }

    public Integer getAdsMonthEnd() {
        return adsMonthEnd;
    }

    public void setAdsMonthEnd(Integer adsMonthEnd) {
        this. adsMonthEnd = adsMonthEnd;
    }

    public Integer getAdsYearEnd() {
        return adsYearEnd;
    }

    public void setAdsYearEnd(Integer adsYearEnd) {
        this.adsYearEnd = adsYearEnd;
    }

    public List<Calendar> getPossibleMonths() {
        String stakeholderName = getStakeholderName();
        if (stakeholderName == null) {
            return null;
        }
        if (this.possibleMonths == null) {
            possibleMonths = pipBean.getSAvailableMonths(stakeholderName);
        }
        return possibleMonths;
    }

    public List<Integer> getStartYears() {
        if (this.startYears == null || this.startYears.isEmpty()) {
            startYears = new ArrayList<Integer>();
            Set temp = new HashSet<Integer>();
            List<Calendar> timeList = this.getPossibleMonths();
            for (Calendar c : timeList) {
                temp.add(c.get(Calendar.YEAR));
            }
            for (Object t : temp) {
                startYears.add((Integer) t);
            }
        }
        Collections.sort(startYears, new Comparator<Integer>() {

            public int compare(Integer i1, Integer i2) {
                if (i1 - i2 > 0) {
                    return 1;
                } else if (i1 - i2 < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        return startYears;
    }

    public List<Integer> getEndYears() {
        if (this.endYears == null || this.endYears.isEmpty()) {
            endYears = new ArrayList<Integer>();
            Set temp = new HashSet<Integer>();
            List<Calendar> timeList = this.getPossibleMonths();
            for (Calendar c : timeList) {
                temp.add(c.get(Calendar.YEAR));
            }
            for (Object t : temp) {
                endYears.add((Integer) t);
            }
        }
        Collections.sort(endYears, new Comparator<Integer>() {

            public int compare(Integer i1, Integer i2) {
                if (i1 - i2 > 0) {
                    return 1;
                } else if (i1 - i2 < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        return endYears;
    }

    public void handleStartYearChange() {
        startMonths = new HashSet<Integer>();
        if (adsYearStart != null) {
            for (Calendar c : this.getPossibleMonths()) {
                if (c.get(Calendar.YEAR) == adsYearStart.intValue()) {
                    startMonths.add(c.get(Calendar.MONTH) + 1);
                }
            }
        }
    }

    public void handleEndYearChange() {
        endMonths = new HashSet<Integer>();
        if (adsYearEnd != null) {
            for (Calendar c : this.getPossibleMonths()) {
                if (c.get(Calendar.YEAR) == adsYearEnd.intValue()) {
                    endMonths.add(c.get(Calendar.MONTH) + 1);
                }
            }
        }
    }

    public Set<Integer> getStartMonths() {
        return startMonths;
    }

    public Set<Integer> getEndMonths() {
        return endMonths;
    }

    public void updateStartEnd(ActionEvent event) {
        start = Calendar.getInstance();
        start.set(adsYearStart, adsMonthStart - 1, 1, 0, 0, 0);
        end = Calendar.getInstance();
        end.set(adsYearEnd, adsMonthEnd - 1, 1, 1, 0, 0);
        System.out.println("start:" + start.get(Calendar.YEAR) + ", " + start.get(Calendar.MONTH));
        System.out.println("end:" + end.get(Calendar.YEAR) + ", " + end.get(Calendar.MONTH));
    }
 
    public void updateNumMonthToPred(ActionEvent event) {
    }

    public String getStakeholderName() {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
    }
}
